package com.baizhi.controller;

import com.baizhi.dto.LoginDTO;
import com.baizhi.po.AdminPO;
import com.baizhi.service.AdminService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RefreshScope
@RestController
public class AdminLoginController {

    private static final Logger log = LoggerFactory.getLogger(AdminLoginController.class);
    @Autowired
    private AdminService adminService;

    @GetMapping("admin-user")
    public AdminPO query(String token) {
        log.info("进入query方法 : {} ", token);
        System.out.println(token);
        AdminPO query = adminService.query(token);
        System.out.println("query = " + query);
        return query;
    }


    @PostMapping("tokens")
    public HashMap<String, String> login(@RequestBody LoginDTO loginDTO) {
        System.out.println("loginDTO = " + loginDTO);
        HashMap<String, Object> map = adminService.queryByUserNameAndPassword(loginDTO);
        Object tokens = map.get("token");
        System.out.println("tokens = " + tokens);
        String token = tokens.toString();
        HashMap<String, String> map1 = new HashMap<>();
        map1.put("token", token);
        return map1;
    }

    @DeleteMapping("tokens/{token}")
    public void deleteToken(@PathVariable("token") String token) {
        log.info("token {}", token);
        adminService.deleteToken(token);
    }
}
